package com.sakura.taos.tool.manipulator.service;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.ds.GroupDataSource;
import com.sakura.taos.tool.manipulator.dao.TaosSysDao;
import com.sakura.taos.tool.manipulator.model.DataBase;
import com.sakura.taos.tool.manipulator.model.DataBaseNews;
import com.sakura.taos.tool.manipulator.model.LayuiTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.*;

import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_MAIN_NAME;
import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_SEPARATOR;
import static com.sakura.taos.tool.manipulator.tool.Tool.listByMapToDataBase;

/**
 * taos数据 库操作
 *  建表等
 * @Author: Sakura
 * @Date: 2021/1/31
 */
@Service
@DS("#header.dsName")
public class TaosBaseService {

    @Autowired
    TaosSysDao taosSys;

    /**
     * 获取某个链接下的库信息,组装成树
     * @param treeName
     * @param dsName
     * @return
     */
    @DS("#dsName")//
    public LayuiTree getTreeData(String treeName,String dsName) {
        List<Map<String,Object>> databasesMap = taosSys.getDatabasesMap();
        List<DataBaseNews> baseNews = listByMapToDataBase(treeName,databasesMap);
        return new LayuiTree(treeName,dsName,baseNews);
    }

    /**
     * 删除库
     * @param dbName
     * @return
     */
    public Integer removeByDbName(String dbName) {
        return taosSys.removeByDbName(dbName);
    }

    public Integer saveDataBase(DataBase dataBase) {
        return taosSys.saveDataBase(dataBase);
    }
}
